load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")

filegroup(
    name = "rbi",
    srcs = glob(
        ["**/*.rbi"],
    ),
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "generate_procs",
    srcs = ["tools/generate_procs.cc"],
    linkstatic = select({
        "//tools/config:linkshared": 0,
        "//conditions:default": 1,
    }),
    visibility = ["//tools:__pkg__"],
)

genrule(
    name = "generate_procs_rbi",
    outs = ["procs.rbi.gen"],
    cmd = "$(location :generate_procs) $@",
    tools = [":generate_procs"],
)

diff_test(
    name = "validate_procs_rbi",
    failure_message = "\n\n\"rbi/procs.rbi\" is autogenerated. Please run:\n    bazel run //rbi:update_procs_rbi\nto update it.\n",
    file1 = "procs.rbi",
    file2 = ":generate_procs_rbi",
)

write_source_file(
    name = "update_procs_rbi",
    in_file = ":generate_procs_rbi",
    out_file = "procs.rbi",
)
